package vtt.android.dapanhc.utils;

import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import vtt.android.library.VttBaseApplication;
import vtt.android.library.utils.VttLog;
import android.os.AsyncTask;

public class HttpRequestUtil {
	private static final String TAG = "HttpRequestUtil";
	private static VttLog log = new VttLog(TAG, VttBaseApplication.isDebuggable);
	private HttpParams httpParameters;
	private static final int CONNECTION_TIME_OUT = 6000;
	private static final int SOCK_TIME_OUT = 6000;
	
	// listener
	private IHttpResponseListener listener;
	private HttpClient httpclient;
	private HttpGet httpGet;
	private HttpPost httppost;
	private ResponseHandler<String> responseHandler;

	public HttpRequestUtil(IHttpResponseListener httpResponseListener) {
		
		this.listener = httpResponseListener;
		
		httpParameters = new BasicHttpParams();
		// Set the timeout in milliseconds until a connection is established.
		// The default value is zero, that means the timeout is not used.
		HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIME_OUT);
		// Set the default socket timeout (SO_TIMEOUT)
		// in milliseconds which is the timeout for waiting for data.
		HttpConnectionParams.setSoTimeout(httpParameters, SOCK_TIME_OUT);
		
		httpclient = new DefaultHttpClient(httpParameters);
		responseHandler = new BasicResponseHandler();
	}
	
	public void addHttpResponseListener(IHttpResponseListener httpResponseListener){
		this.listener = httpResponseListener;
	}

	public void makeRequestGET(String uri, String userAgent) {
		log.d("VTT >>> makeRequestGET uri: " + uri);
		log.d("VTT >>> makeRequestGET userAgent: " + userAgent);
		try {
			httpGet = new HttpGet(uri);
			if(!userAgent.isEmpty()){
				httpGet.setHeader("User-Agent", userAgent);
			}
			new RequestTask(httpGet).execute(uri);
		} catch (Exception e) {
			log.e("VTT>>> makeRequestGET err >>>> " + e.getMessage());
			e.printStackTrace();
		}
	}

	public void makeRequestPOST(String uri, List<NameValuePair> nameValuePairs, String userAgent) {
		log.d("VTT>>> makeRequestPOST  uri: " + uri);
		log.d("VTT>>> makeRequestPOST  userAgent: " + userAgent);
		try {
			httppost = new HttpPost(uri);
			if(!userAgent.isEmpty()){
				httppost.setHeader("User-Agent", userAgent);
			}
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			new RequestTask(httppost).execute(uri);
		} catch (Exception e) {
			log.e("VTT>>> makeRequestPOST err >>>> " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	class RequestTask extends AsyncTask<String, Void, String>{
		
		HttpUriRequest httpUriRequest;
		
		public RequestTask(HttpUriRequest httpRequest) {
			httpUriRequest = httpRequest;
		}

		@Override
		protected String doInBackground(String... params) {
			try {
				return httpclient.execute(httpUriRequest, responseHandler);
			} catch (Exception e){
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			if(result != null){
				listener.onSuccess(result);
			} else {
				listener.onFail();
			}
		}
	}
	
	public interface IHttpResponseListener {
		public void onSuccess(Object result);
		public void onFail();
	}
}
